/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModificarArticulo.java
 *
 * Created on 02/08/2011, 20:16:39
 */

package ventanas;

import conexiones.AdministradorDeConexiones;
import dao.ArticulosDao;
import dao.RubrosDao;
import java.sql.Connection;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import modelo.Articulo;
import modelo.Perfil;
import modelo.Rubro;

/**
 *
 * @author Cristian Perez
 */
public class ModificarArticulo extends javax.swing.JDialog {
    private DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();

    /** Creates new form ModificarArticulo */
    public ModificarArticulo(JDialog parent, boolean modal) {
        super(parent, modal);
        initComponents();

        jComboRubros.setModel(modeloCombo);
        modeloCombo = (DefaultComboBoxModel) jComboRubros.getModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        lblDescrip = new javax.swing.JLabel();
        lblPcosto = new javax.swing.JLabel();
        lblPventa = new javax.swing.JLabel();
        lblStkMin = new javax.swing.JLabel();
        lblStkAct = new javax.swing.JLabel();
        lblRubro = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtDescrip = new javax.swing.JTextField();
        txtPcosto = new javax.swing.JTextField();
        txtPventa = new javax.swing.JTextField();
        txtStkMin = new javax.swing.JTextField();
        txtStkAct = new javax.swing.JTextField();
        jComboRubros = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblPorcVenta = new javax.swing.JLabel();
        txtPorcVenta = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnGuardar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificacion de Articulos");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblCodigo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblCodigo.setText("Codigo :");

        lblDescrip.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblDescrip.setText("Descripcion: ");

        lblPcosto.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPcosto.setText("Precio Costo: ");

        lblPventa.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPventa.setText("Precio Venta: ");

        lblStkMin.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblStkMin.setText("Stock Minimo: ");

        lblStkAct.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblStkAct.setText("Stock Actual: ");

        lblRubro.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblRubro.setText("Rubro: ");

        txtCodigo.setBackground(new java.awt.Color(176, 199, 245));
        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Tahoma", 1, 11));

        txtDescrip.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtDescrip.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDescripKeyPressed(evt);
            }
        });

        txtPcosto.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtPcosto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPcostoKeyPressed(evt);
            }
        });

        txtPventa.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtPventa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPventaKeyPressed(evt);
            }
        });

        txtStkMin.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtStkMin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtStkMinKeyPressed(evt);
            }
        });

        txtStkAct.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtStkAct.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtStkActKeyPressed(evt);
            }
        });

        jComboRubros.setFont(new java.awt.Font("Tahoma", 1, 11));
        jComboRubros.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jComboRubrosKeyPressed(evt);
            }
        });

        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("*");

        jLabel3.setForeground(new java.awt.Color(255, 0, 51));
        jLabel3.setText("*");

        jLabel4.setForeground(new java.awt.Color(255, 0, 51));
        jLabel4.setText("*");

        jLabel5.setForeground(new java.awt.Color(255, 0, 51));
        jLabel5.setText("*");

        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("(*) Campos obligatorios");

        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("*");

        lblPorcVenta.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPorcVenta.setText("% de Venta: ");

        txtPorcVenta.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtPorcVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPorcVentaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPventa)
                            .addComponent(lblStkMin)
                            .addComponent(lblStkAct)
                            .addComponent(lblRubro))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jComboRubros, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtStkAct, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtStkMin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(txtPventa, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addGap(223, 223, 223))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCodigo)
                            .addComponent(lblDescrip)
                            .addComponent(lblPcosto))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtPcosto, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtDescrip, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtPorcVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblPorcVenta)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(163, 163, 163)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(208, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodigo)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDescrip)
                    .addComponent(txtDescrip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPcosto)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPcosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPorcVenta)
                    .addComponent(txtPorcVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPventa)
                    .addComponent(txtPventa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStkMin)
                    .addComponent(txtStkMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStkAct)
                    .addComponent(txtStkAct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRubro)
                    .addComponent(jComboRubros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnGuardar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnSalir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(85, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-514)/2, (screenSize.height-481)/2, 514, 481);
    }// </editor-fold>//GEN-END:initComponents

    private void txtDescripKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescripKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                txtPcosto.requestFocus();
            }
        }
    }//GEN-LAST:event_txtDescripKeyPressed

    private void txtPcostoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPcostoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                txtPorcVenta.requestFocus();
            }
        }
    }//GEN-LAST:event_txtPcostoKeyPressed

    private void txtPventaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPventaKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                txtStkMin.requestFocus();
            }
        }
    }//GEN-LAST:event_txtPventaKeyPressed

    private void txtStkMinKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStkMinKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                txtStkAct.requestFocus();
            }
        }
    }//GEN-LAST:event_txtStkMinKeyPressed

    private void txtStkActKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStkActKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                jComboRubros.requestFocus();
            }
        }
    }//GEN-LAST:event_txtStkActKeyPressed

    private void jComboRubrosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jComboRubrosKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            btnGuardar.requestFocus();
        }
    }//GEN-LAST:event_jComboRubrosKeyPressed

    private void txtPorcVentaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPorcVentaKeyPressed
        // TODO add your handling code here:
         if (evt.getKeyCode() == 10) {
          if (validarPrecios()) {
            try {
                float porciento = Float.parseFloat(txtPorcVenta.getText().trim());
                float preCosto = Float.parseFloat(txtPcosto.getText().trim());
                //Paso el paseo, es un numero
                txtPventa.setText(String.valueOf(preCosto + (preCosto * porciento / 100)));
                txtStkMin.requestFocus();
            }catch(Exception e){
                e.printStackTrace();
            }
          }
        }
    }//GEN-LAST:event_txtPorcVentaKeyPressed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if (validarFormulario()) {
            Connection conn = null;
            try {
                conn = AdministradorDeConexiones.obtenerConexion();
                long codigo = Long.parseLong(txtCodigo.getText());
                String desc = txtDescrip.getText();
                float preCosto = Float.parseFloat(txtPcosto.getText().trim());
                float preVenta = Float.parseFloat(txtPventa.getText().trim());
                float porcentaje = Float.parseFloat(txtPorcVenta.getText().trim());
                int stkMin = Integer.parseInt(txtStkMin.getText());
                int stkAct = Integer.parseInt(txtStkAct.getText());
                Rubro rub =(Rubro)jComboRubros.getSelectedItem();
                Articulo art = new Articulo(codigo, desc, preCosto, porcentaje, preVenta, stkAct, stkMin, rub);
                ArticulosDao.getInstance().modificar(conn, art);
                JOptionPane.showMessageDialog(this, "El articulo ha sido guardado!!!", "Guardar", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }catch(Exception e) {e.printStackTrace();}
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox jComboRubros;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblDescrip;
    private javax.swing.JLabel lblPcosto;
    private javax.swing.JLabel lblPorcVenta;
    private javax.swing.JLabel lblPventa;
    private javax.swing.JLabel lblRubro;
    private javax.swing.JLabel lblStkAct;
    private javax.swing.JLabel lblStkMin;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescrip;
    private javax.swing.JTextField txtPcosto;
    private javax.swing.JTextField txtPorcVenta;
    private javax.swing.JTextField txtPventa;
    private javax.swing.JTextField txtStkAct;
    private javax.swing.JTextField txtStkMin;
    // End of variables declaration//GEN-END:variables


    //Verifica que los valores ingresados en los campos sean correctos
    private boolean validarFormulario() {
        //Validamos que el campo descripcion no este vacio
        if (txtDescrip.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar una descripcion...", "Error", JOptionPane.WARNING_MESSAGE);
            txtDescrip.requestFocus();
            return false;
        }

        //Validamos que el campo precio costo no este vacio
        if (txtPcosto.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de costo...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPcosto.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el precio costo no sea un numero real
        try {
            Double.parseDouble(txtPcosto.getText());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de costo debe ser un numero real!", "Error", JOptionPane.WARNING_MESSAGE);
            txtPcosto.requestFocus();
            txtPcosto.setText("");
            return false;
        }

        //Validamos que el campo precio venta no este vacio
        if (txtPventa.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de venta...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPventa.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el precio venta no sea un numero real
        try {
            Double.parseDouble(txtPventa.getText());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de venta debe ser un numero real!", "Error", JOptionPane.WARNING_MESSAGE);
            txtPventa.requestFocus();
            txtPventa.setText("");
            return false;
        }

        //Validamos que el valor del campo stock minimo sea correcto
        if (txtStkMin.getText().equals("")) {
            txtStkMin.setText("0");
        }

        try {
                Integer.parseInt(txtStkMin.getText());
           }catch(Exception NumberFormatException) {
                JOptionPane.showMessageDialog(this, "El stock minimo debe ser un numero entero!", "Error", JOptionPane.WARNING_MESSAGE);
                txtStkMin.requestFocus();
                txtStkMin.setText("");
                return false;
           }
        

         //Validamos que el valor del campo stock actual sea correcto
        if (txtStkAct.getText().equals("")) {
            txtStkAct.setText("0");
        }
           try {
                Integer.parseInt(txtStkAct.getText());
           }catch(Exception NumberFormatException) {
                JOptionPane.showMessageDialog(this, "El stock actual debe ser un numero entero!", "Error", JOptionPane.WARNING_MESSAGE);
                txtStkAct.requestFocus();
                txtStkAct.setText("");
                return false;
           }
        

        //Validamos que existan rubros cargados en la base de datos
        if (jComboRubros.getItemCount() == 0) {
            JOptionPane.showMessageDialog(this, "No existen rubros, debe cargarlas antes de ingresar articulos", "Error", JOptionPane.WARNING_MESSAGE);
            return false;
        }
       return true;
    }

    public void mostrarUsuario (Articulo articulo) {
        txtCodigo.setText(String.valueOf(articulo.getCod()));
        txtDescrip.setText(articulo.getDescrip());
        txtPcosto.setText(String.valueOf(articulo.getPreCosto()));
        txtPorcVenta.setText(String.valueOf(articulo.getPorc()));
        txtPventa.setText(String.valueOf(articulo.getPreVenta()));
        txtStkMin.setText(String.valueOf(articulo.getStkMin()));
        txtStkAct.setText(String.valueOf(articulo.getStkAct()));
        cargarComboRubros(articulo.getRubro());
    }

    private void cargarComboRubros(Rubro rubro) {
        Connection conn = null;
        ArrayList lista = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            lista = RubrosDao.getInstance().getTodos(conn);
            Rubro rub = null;
            for (int i=0; i < lista.size(); i++) {
                rub = (Rubro)lista.get(i);
                modeloCombo.addElement(rub);
            }
            modeloCombo.setSelectedItem(rubro);
        }catch(Exception e) {e.printStackTrace();}
    }

    private boolean validarPrecios() {
        //Validamos que el campo precio costo no este vacio
        if (txtPcosto.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de costo...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPcosto.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el precio costo no sea un numero real
        try {
            Double.parseDouble(txtPcosto.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de costo debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPcosto.requestFocus();
            txtPcosto.setText("");
            return false;
        }

        //Validamos que el campo porcentaje de venta no este vacio
        if (txtPorcVenta.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un porcentaje de venta...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPorcVenta.setText("");
            txtPorcVenta.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el porcentaje de venta no sea un numero
        try {
            Double.parseDouble(txtPorcVenta.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El porcentaje de venta debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPcosto.requestFocus();
            txtPcosto.setText("");
            return false;
        }

       return true;
   }
}
